<?php

class UserIdentity extends CUserIdentity {

    public $id;

    const ERROR_EMAIL_INVALID = 3;
    const ERROR_STATUS_NOTACTIVE = 4;
    const ERROR_STATUS_BANNED = 5;
    const ERROR_STATUS_REMOVED = 6;

    public function authenticate() {
        $user = User::model()->find('LOWER(email) = :email', array(':email' => strtolower($this->username)));
        if($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else {
            if(User::encrypt($this->password) !== $user->password)
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
            else if($user->status == User::STATUS_NOTACTIVE)
                $this->errorCode = self::ERROR_STATUS_NOTACTIVE;
            else if($user->status == User::STATUS_BANNED)
                $this->errorCode = self::ERROR_STATUS_BANNED;
            else {
                $this->id = $user->id;
                $this->username = $user->username;
                $this->errorCode = self::ERROR_NONE;
            }
        }
        return !$this->errorCode;
    }

    public function getId() {
        return $this->id;
    }
}
